public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;

    //查找某一节点元素
    public TreeNode find(int val) {
        TreeNode cur = root;
        while(cur != null) {
            if (cur.val == val) {
                return cur;
            }else if (cur.val < val) {
                cur = cur.left;
            }else {
                cur = cur.right;
            }
        }
        return null;
    }

    //插入元素
    public void insert(int val) {
        if (root == null) {
            root = new TreeNode(val);
            return;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            parent = cur;
            if (cur.val == val) {
                return;
            }else if (cur.val < val) {
                cur = cur.left;
            }else {
                cur = cur.right;
            }
        }
        TreeNode node = new TreeNode(val);
        if (parent.val > val) {
            parent.left = node;
        }else {
            parent.right = node;
        }
    }
}
